package com.tony.demo.ui.framework;

import android.view.View;

import com.puhui.lib.utils.DMLog;
import com.tony.demo.R;
import com.tony.demo.mvp.presenter.interfaces.IBasePresenter;
import com.tony.demo.ui.BaseFragment;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 投资
 */
public class FrameworkFragment extends BaseFragment {

    @Override
    public IBasePresenter createPresenter() {
        return null;
    }


    @Override
    protected int getViewLayout() {
        return R.layout.fragment_framwork;
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rx_java_learn:
                doRxJavaTest();
//                doRxJavaTestTwo();
                break;
        }
    }

    private void doRxJavaTestTwo() {
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {

            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe();
    }

    private void doRxJavaTest() {

        Observable<Integer> just = Observable.just(1);
        DMLog.e(this.getClass().getSimpleName(), just.getClass().getName());

        // ObservableSubscribeOn 与 ObservableObserveOn 哪个放在前面，哪个的subscribeActual方法就后执行
        //ObservableSubscribeOn subscribeActual
        Observable<Integer> subscribeOn = just.subscribeOn(Schedulers.io());
        DMLog.e(this.getClass().getSimpleName(), subscribeOn.getClass().getName());

        //ObservableObserveOn subscribeActual
        Observable<Integer> observeOn = subscribeOn.observeOn(AndroidSchedulers.mainThread());
        DMLog.e(this.getClass().getSimpleName(), observeOn.getClass().getName());

        observeOn.subscribe(new Observer<Integer>() {
            @Override
            public void onSubscribe(Disposable d) {
                DMLog.e(this.getClass().getSimpleName(), "onSubscribe " + Thread.currentThread().getName());
            }

            @Override
            public void onNext(Integer integer) {
                DMLog.e(this.getClass().getSimpleName(), "onNext " + Thread.currentThread().getName());
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {
                DMLog.e(this.getClass().getSimpleName(), "onComplete " + Thread.currentThread().getName());
            }
        });
    }
}
